define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'news/index' + location.search,
                    add_url: 'news/add',
                    edit_url: 'news/edit',
                    del_url: 'news/del',
                    multi_url: 'news/multi',
                    import_url: 'news/import',
                    table: 'news',
                }
            });
            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'desc', title: __('Desc'), operate: 'LIKE'},
                      //  {field: 'type', title: __('Type'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},

                        {field: 'send_type', title: __('Send_type'), searchList: {"1":__('Send_type 1'),"2":__('Send_type 2')}, formatter: Table.api.formatter.normal},
                    //    {field: 'user_ids', title: __('User_ids'), operate: 'LIKE'},
                      //  {field: 'reward', title: __('Reward'), operate: 'LIKE'},
                        {field: 'admin_id', title: __('Admin_id')},
                        {field: 'is_send', title: __('是否发送'), searchList: {"0":__('未发送'),"1":__('已发送')}, formatter: Table.api.formatter.status},
                        {field: 'send_time', title: __('发送时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'created_time', title: __('Created_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updated_time', title: __('Updated_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Table.api.init( );
            var table2 = $("#table2");
            table2.bootstrapTable({
                url: 'game_ranking',
                pk: 'player_id',
                toolbar: '#toolbar2',
                sortName: 'player_id',
                search: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,search:false},
                        {field: 'player_id', title: 'ID',search:false},
                        {field: 'player_name', title: __('Nickname'),search:false},
                        {field: 'coin', title: __('金币'),search:false},
                    ]
                ]
            });

            var table3 = $("#table3");
            table3.bootstrapTable({
                url: 'game_ranking/week',
                pk: 'player_id',
                toolbar: '#toolbar3',
                sortName: 'player_id',
                search: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,search:false},
                        {field: 'player_id', title: 'ID',search:false},
                        {field: 'player_name', title: __('Nickname'),search:false},
                        {field: 'coin', title: __('金币'),search:false},
                    ]
                ]
            });

            var table4 = $("#table4");
            table3.bootstrapTable({
                url: 'game_ranking/month',
                pk: 'player_id',
                toolbar: '#toolbar4',
                sortName: 'player_id',
                search: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,search:false},
                        {field: 'player_id', title: 'ID',search:false},
                        {field: 'player_name', title: __('Nickname'),search:false},
                        {field: 'coin', title: __('金币'),search:false},
                    ]
                ]
            });

            // 获取选中项
            $(document).on("click", "#aabb2", function () {
                console.log(Table.api.selectedids(table2));
                var ids = Table.api.selectedids(table2);
                $("#user_ids").html(ids.join(" "));
            });
            $(document).on("click", "#aabb3", function () {
                console.log(Table.api.selectedids(table3));
                var ids = Table.api.selectedids(table3);
                $("#user_ids").html(ids.join(" "));
            });
            $(document).on("click", "#aabb4", function () {
                console.log(Table.api.selectedids(table4));
                var ids = Table.api.selectedids(table4);
                $("#user_ids").html(ids.join(" "));
            });

            // 为表格2绑定事件
            Table.api.bindevent(table2);
            Table.api.bindevent(table3);
            Table.api.bindevent(table4);
            Controller.api.bindevent();
        },
        edit: function () {
            Table.api.init( );
            var table2 = $("#table2");
            table2.bootstrapTable({
                url: 'game_ranking',
                pk: 'player_id',
                toolbar: '#toolbar2',
                sortName: 'player_id',
                search: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,search:false},
                        {field: 'player_id', title: 'ID',search:false},
                        {field: 'player_name', title: __('Nickname'),search:false},
                        {field: 'coin', title: __('金币'),search:false},
                    ]
                ]
            });

            var table3 = $("#table3");
            table3.bootstrapTable({
                url: 'game_ranking/week',
                pk: 'player_id',
                toolbar: '#toolbar3',
                sortName: 'player_id',
                search: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,search:false},
                        {field: 'player_id', title: 'ID',search:false},
                        {field: 'player_name', title: __('Nickname'),search:false},
                        {field: 'coin', title: __('金币'),search:false},
                    ]
                ]
            });

            var table4 = $("#table4");
            table3.bootstrapTable({
                url: 'game_ranking/month',
                pk: 'player_id',
                toolbar: '#toolbar4',
                sortName: 'player_id',
                search: false,
                columns: [
                    [
                        {field: 'state', checkbox: true,search:false},
                        {field: 'player_id', title: 'ID',search:false},
                        {field: 'player_name', title: __('Nickname'),search:false},
                        {field: 'coin', title: __('金币'),search:false},
                    ]
                ]
            });

            // 获取选中项
            $(document).on("click", "#aabb2", function () {
                console.log(Table.api.selectedids(table2));
                var ids = Table.api.selectedids(table2);
                $("#user_ids").html(ids.join(" "));
            });
            $(document).on("click", "#aabb3", function () {
                console.log(Table.api.selectedids(table3));
                var ids = Table.api.selectedids(table3);
                $("#user_ids").html(ids.join(" "));
            });
            $(document).on("click", "#aabb4", function () {
                console.log(Table.api.selectedids(table4));
                var ids = Table.api.selectedids(table4);
                $("#user_ids").html(ids.join(" "));
            });

            // 为表格2绑定事件
            Table.api.bindevent(table2);
            Table.api.bindevent(table3);
            Table.api.bindevent(table4);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },

    };
    return Controller;
});
